package gui.nodes;

import gui.AbstractViewPane;
import gui.MainController;
import gui.SEEUIConsulter;
import gui.ServiceUIConsulter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import utility.Record;
import business.SEEBL;

public class SEENode extends AbstractNode {

	int seeId;
	String seeName;
	int matiereId;
	DefaultMutableTreeNode SEENode;
	AbstractNode parentNode;

	public SEENode(AbstractNode parentNode, Record seeRec, int matiereId) {
		super();
		this.parentNode = parentNode;
		this.seeId = seeRec.getId();
		this.seeName = seeRec.getLibelle();
		this.matiereId = matiereId;
	}

	@Override
	public JPopupMenu makePopup() {
		return new Popup();
	}

	@Override
	public String getNodeName() {
		return seeName;
	}

	public void loadPrimaryViewPane() {
		AbstractViewPane newViewPane = new SEEUIConsulter(parentNode,
AbstractViewPane.ViewMode.CONSULTATION, seeId, matiereId);
		MainController.getInstance().setViewPane(newViewPane);
		}	
	
	public void loadModifierViewPane() {
		AbstractViewPane newViewPane = new SEEUIConsulter(parentNode,
AbstractViewPane.ViewMode.MODIFICATION, seeId, matiereId);
		MainController.getInstance().setViewPane(newViewPane);
		}
	public void loadSupprimerViewPane() {
		if (supprimer() == JOptionPane.YES_OPTION) {
			SEEBL seeBL = new SEEBL(seeId);
			seeBL.supprimerSEE();
		}
		((AbstractNode)parent).refresh();
	}
	
	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new ServiceUIConsulter(parentNode, AbstractViewPane.ViewMode.AJOUT, seeId, matiereId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter un service");
		JMenuItem modifier = new JMenuItem("Modifier le SEE");
		JMenuItem supprimer = new JMenuItem("Supprimer le SEE");

		public Popup() {
			ajouter.addActionListener(this);
			modifier.addActionListener(this);
			supprimer.addActionListener(this);
			add(ajouter);
			add(modifier);
			add(supprimer);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter) {
				loadAjouterViewPane();
			}
			if (e.getSource() == modifier) {
				loadModifierViewPane();
			}
			if (e.getSource() == supprimer) {
				loadSupprimerViewPane();
			}
		}
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();
		
		SEEBL seeBL = new SEEBL(seeId);
		
		for (Record serviceRec : seeBL.getListeService()) {
			ServiceNode serviceNode = new ServiceNode(parentNode, serviceRec, seeId);
			add(serviceNode);
		}
	}
	
	public int supprimer() {
		return JOptionPane.showConfirmDialog(
			    null,
			    "Êtes vous sûr de vouloir supprimer le SEE\n"+
			    //ensBL.getName()+" "+ensBL.getPrenom()+
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		
	}

}
